package com.ad.platform.service.impl;

import com.ad.payload.dto.JielongTimeZoneDto;
import com.ad.payload.model.IdDto;
import com.ad.payload.vo.JielongTimeZoneVo;
import com.ad.platform.common.utils.BeanUtil;
import com.ad.platform.mapper.JielongTimeZoneMapper;
import com.ad.platform.mapper.PreOrderMapper;
import com.ad.platform.model.JielongTimeZone;
import com.ad.platform.model.PreOrder;
import com.ad.platform.service.IJielongTimeZoneService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rclia
 * @since 2021-04-05
 */
@Service
@Slf4j
public class JielongTimeZoneServiceImpl extends ServiceImpl<JielongTimeZoneMapper, JielongTimeZone> implements IJielongTimeZoneService {

    @Autowired
    private JielongTimeZoneMapper jielongTimeZoneMapper;

    @Autowired
    private PreOrderMapper preOrderMapper;

    @Override
    public List<JielongTimeZoneVo> listTimeZoneByMerchantId(@Valid IdDto idDto) {
        log.info("入参 ============================================ " + JSON.toJSONString(idDto));
        QueryWrapper<JielongTimeZone> jielongTimeZoneQueryWrapper = new QueryWrapper<>();
        jielongTimeZoneQueryWrapper.eq("merchant_id",idDto.getId());
        jielongTimeZoneQueryWrapper.orderByAsc("start_time");
        List<JielongTimeZone> jielongTimeZones = jielongTimeZoneMapper.selectList(jielongTimeZoneQueryWrapper);

        String jielongCreateSuccess = "Y";
        if(!StringUtils.isBlank(idDto.getJielongId())){
            QueryWrapper<PreOrder> preOrderQueryWrapper = new QueryWrapper<>();
            preOrderQueryWrapper.eq("order_id",idDto.getJielongId());
            PreOrder preOrder = preOrderMapper.selectOne(preOrderQueryWrapper);
            if(preOrder == null){
                //创建接龙订单失败
                jielongCreateSuccess = "N";
            }
        }

        // 实体转vo
        List<JielongTimeZoneVo> rtnList = new ArrayList();
        for(JielongTimeZone model : jielongTimeZones){
            JielongTimeZoneVo vo = new JielongTimeZoneVo();
            BeanUtil.copyProperties(model, vo);
            vo.setJielongCreateSuccess(jielongCreateSuccess);
            rtnList.add(vo);
        }

        return rtnList;
    }

    @Override
    public boolean saveOrUpdate(@Valid JielongTimeZoneDto jielongTimeZoneDto) {
        return true;
    }

    @Override
    public boolean delTimeZone(@Valid IdDto idDto) {
        return false;
    }
}
